//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.滑动窗口.定长滑动窗口.未解决;

public class demo_1888 {
    public demo_1888() {
    }

    public static void main(String[] args) {
        System.out.println(minFlips("111000"));
    }

    public static int minFlips(String s) {
        StringBuilder sDouble = new StringBuilder();
        sDouble.append(s);
        sDouble.append(s);
        char[] chars = new char[sDouble.length()];

        for(int i = 0; i < sDouble.length(); ++i) {
            if (sDouble.charAt(i) - 48 == i % 2) {
                chars[i] = '.';
            } else {
                chars[i] = 'x';
            }
        }

        int n = s.length();
        int k = s.length();
        int sumY = 0;
        int sumF = 0;
        int min = n;
        int left = 0;

        for(int i = 0; i < chars.length; ++i) {
            if (chars[i] == '.') {
                ++sumY;
            } else {
                ++sumF;
            }

            if (i >= k - 1) {
                min = Math.min(min, Math.min(sumF, sumY));
                if (chars[left] == '.') {
                    --sumY;
                } else {
                    ++sumF;
                }
            }
        }

        return min;
    }
}
